package com.uzai.console.queue;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 *
 */
@Data
public class ConsoleDelayTask implements Delayed {

    private final Long taskId;
    private final Long delay;
    private final String name;
    private final Runnable run;

    public ConsoleDelayTask(Long taskId, Integer delay, String name, Runnable run) {
        this.taskId = taskId;
        this.delay = delay * 1000L + System.currentTimeMillis();
        this.name = name;
        this.run = run;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(delay - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return Long.compare(this.getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS));
    }

    @Override
    public String toString() {
        return "ConsoleDelayTask{" +
                "delay=" + delay +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ConsoleDelayTask that = (ConsoleDelayTask) o;
        return taskId.equals(that.taskId);
    }

    @Override
    public int hashCode() {
        return taskId.hashCode();
    }
}
